package com.connectrenren;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

public class Start {

    private static final String API_KEY = "81498b25cf75481cbf0482a359843fc6";
    private static final String API_SECRET = "e631c76f383a47e68659a21df2a33cf8";
    private static final String APP_ID = "144643";
    private static final String USER_NAME = "renlee";
    private static final String PASSWORD = "youcantseeme";

    private static final String AUTHORIZE_URL = "https://graph.renren.com/oauth/authorize";
    private static final String CLIENT_ID = "client_id";
    private static final String RESPONSE_TYPE = "response_type";
    private static final String REDIRECT_URI = "redirect_uri";
    private static final String SCOPE = "scope";
    private static final String STATE = "state";
    private static final String DISPLAY = "display";
    private static final String TOKEN = "token";
    private static final String UTF8 = "utf-8";
    private static final String USER_AGENT = "User_Agent";

    private static final String USER_AGENT_VALUE = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

    private static final Logger logger = Logger.getLogger(Start.class);
    
    public String getSessionKey() throws ClientProtocolException, IOException {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(AUTHORIZE_URL);
        String url = "http://graph.renren.com/oauth/login_success.html";
        List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair(CLIENT_ID, API_KEY));
        parameters.add(new BasicNameValuePair(RESPONSE_TYPE, TOKEN));
        parameters.add(new BasicNameValuePair(REDIRECT_URI, url));
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, UTF8);
        post.setEntity(formEntity);
        post.setHeader(USER_AGENT, USER_AGENT_VALUE);
        
        HttpResponse response = client.execute(post);
        HttpEntity entity = response.getEntity();
        response.getStatusLine();
        logger.debug(post.getURI());
        Header[] headers = response.getHeaders("location");
        String redirect_url = headers[0].getValue();
        logger.debug(headers[0].getValue());
        logger.debug(response.getStatusLine());
        post.abort();
        HttpGet get = new HttpGet(redirect_url);
        get.setHeader(USER_AGENT, USER_AGENT_VALUE);
        response = client.execute(get);
        logger.debug(response.getStatusLine());
        
        get.abort();
        String url2 = Login(client);
        getAccessToken(client, url2);
        return null;
    }

    private String Login(HttpClient client) throws UnsupportedEncodingException,
            IOException, ClientProtocolException {
        String login_uri = "http://passport.renren.com/RL.do?p3p=1";
        HttpPost post2 = new HttpPost(login_uri);
        List<NameValuePair> parameters2 = new ArrayList<NameValuePair>();
        parameters2.add(new BasicNameValuePair("email", "renlee"));
        parameters2.add(new BasicNameValuePair("password", "youcantseeme"));
        parameters2.add(new BasicNameValuePair("domain", "renren.com"));
        parameters2.add(new BasicNameValuePair("origURL", "http://graph.renren.com/oauth/authorize?&pp=1&https=https://graph.renren.com"));
        UrlEncodedFormEntity formEntity2 = new UrlEncodedFormEntity(parameters2, UTF8);
        post2.setEntity(formEntity2);
        post2.setHeader(USER_AGENT, USER_AGENT_VALUE);
        HttpResponse response2 = client.execute(post2);
        logger.debug(response2.getStatusLine());
        Header[] headers2 = response2.getHeaders("location");
        String redirect_url2 = headers2[0].getValue();
        logger.debug("redirect_url2" + redirect_url2);
        post2.abort();
        return redirect_url2;
    }
    
    private void getAccessToken(HttpClient client, String url) throws ClientProtocolException, IOException
    {
//        <input type="hidden" value="81498b25cf75481cbf0482a359843fc6" name="client_id">
//        <input type="hidden" value="http://graph.renren.com/oauth/login_success.html" name="redirect_uri">
//        <input type="hidden" value="token" name="response_type">
//        <input type="hidden" value="" name="state">
//        <input type="hidden" value="[]" name="scope">
//        <input type="hidden" value="80005" name="origin">
//        <input type="hidden" value="page" name="display">
//        <input id="connectFeed" type="hidden" value="true" name="connectFeed">
//        <input id="follow" type="hidden" value="true" name="follow">
//        <input id="email" type="hidden" value="true" name="email">
//        <input type="hidden" value="fe6095269bc4f4d737760549053e17bb-60-1307696573346" name="post_form_id">
//        String uri = "https://graph.renren.com/user_approve";
        HttpGet get = new HttpGet(url);
        get.setHeader(USER_AGENT, USER_AGENT_VALUE);
        HttpResponse response = client.execute(get);
        logger.debug(response.getStatusLine());
        get.abort();
        
        
    }

}